package com.cq.androidtools.room1

import android.content.Context
import androidx.room.*
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import androidx.sqlite.db.SupportSQLiteOpenHelper

/**
 * 作者: CQ
 * 日期: 2021-04-22
 * 说明: 数据库文件
 */
@Database(
    entities = [Book::class, Student::class, MakeBean::class],
    version = 7,
    exportSchema = false
)
abstract class AppDataBase : RoomDatabase() {

    // BookDao
    abstract fun bookDao():BookDao
    abstract fun studentDao():StudentDao
    abstract fun userDao():UserDao
    abstract fun makeDao():MakeDao


    companion object{
        @Volatile
        private var instance:AppDataBase? = null

        fun getInstance(context: Context):AppDataBase{
            return instance?: synchronized(this){
                instance?:buildDataBase(context)
                    .also {
                        instance = it
                    }
            }
        }

        private fun buildDataBase(context: Context):AppDataBase{
            return Room
                .databaseBuilder(context,AppDataBase::class.java,"jetPackDemo-database")
                .addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4, MIGRATION_4_5, MIGRATION_5_6, MIGRATION_6_7)
                .addCallback(object :RoomDatabase.Callback(){
                    override fun onCreate(db: SupportSQLiteDatabase) {
                        super.onCreate(db)
                        // 读取鞋的集合
//                        val request = OneTimeWorkRequestBuilder<ShoeWorker>().build()
//                        WorkManager.getInstance(context).enqueue(request)
                    }
                })
                .build()
        }

        val MIGRATION_1_2 = object : Migration(1 ,2) {
            override fun migrate(database: SupportSQLiteDatabase) {
            }
        }
        val MIGRATION_2_3 = object : Migration(2 ,3) {
            override fun migrate(database: SupportSQLiteDatabase) {
                    database.execSQL("ALTER TABLE Book add COLUMN times INTEGER NOT NULL DEFAULT 0")
            }
        }

        val MIGRATION_3_4 = object : Migration(3 ,4) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL("CREATE TABLE IF NOT EXISTS Students(sId INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name1 TEXT NOT NULL, age INTEGER NOT NULL)")
                database.execSQL("insert into Students(sId, name1, age) select sId, name, age from Student")
                database.execSQL("drop table Student")
                database.execSQL("alter table Students rename to Student")
            }
        }
        val MIGRATION_4_5 = object : Migration(4 ,5) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL("CREATE TABLE IF NOT EXISTS BookS(bookId INTEGER PRIMARY KEY AUTOINCREMENT, bookName TEXT NOT NULL, studentId INTEGER NOT NULL, times INTEGER NOT NULL)")
                database.execSQL("insert into BookS(bookId, bookName, studentId, times) select bookId, bookName, studentId, times from Book")
                database.execSQL("drop table Book")
                database.execSQL("alter table BookS rename to Book")
            }
        }
        val MIGRATION_5_6 = object : Migration(5 ,6) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL("CREATE TABLE IF NOT EXISTS BookS(bookId INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, bookName TEXT NOT NULL, studentId INTEGER NOT NULL, times INTEGER NOT NULL)")
                database.execSQL("insert into BookS(bookId, bookName, studentId, times) select bookId, bookName, studentId, times from Book")
                database.execSQL("drop table Book")
                database.execSQL("alter table BookS rename to Book")
            }
        }
        val MIGRATION_6_7 = object : Migration(6 ,7) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL("CREATE TABLE IF NOT EXISTS Make(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, sId INTEGER NOT NULL, type TEXT NOT NULL, cj TEXT NOT NULL)")
            }
        }
    }

    override fun createOpenHelper(config: DatabaseConfiguration?): SupportSQLiteOpenHelper {
        TODO("Not yet implemented")
    }

    override fun createInvalidationTracker(): InvalidationTracker {
        TODO("Not yet implemented")
    }

    override fun clearAllTables() {
        TODO("Not yet implemented")
    }
}